package com.kfm.am;

import java.sql.*;
import java.util.Random;

public class Test02 {

    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");

        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/manager", "root", "");
            String sql = "{call test_sum2(?, ?)}";
            // 获取执行存储过程的对象
            CallableStatement call = connection.prepareCall(sql);
            // 替换占位符  IN 输入参数 setXxxx() Xxxx取决于IN参数的数据类型 OUT 输出参数 registerOutParameter(index, 输出参数的类型) --> Types.Integer Types.Double
            call.setInt(1, 100);
            call.registerOutParameter(2, Types.INTEGER);
            // 执行存储过程 存储过程中没有OUT参数 把结果用查询的形式展示了一个结果集
//            ResultSet set = call.executeQuery(); // call test_insert2(500, '小李'); 存在结果集
//            if (set.next()) {
//                System.out.println("插入成功， id为：" + set.getObject(1));
//            }
            call.execute();
            int sum = call.getInt(2);
            System.out.println(sum);
            call.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(new Random().nextInt(39) + 1);
    }
}
